home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / bublsort.asm < prev    next >
Assembly Source File  |  1985-06-03  |  3KB  |  64 lines

  1. cseg    segment            ;tell assembler we are starting a segment
  2. assume cs:cseg            ;all assembler needs to know
  3. sort    proc    far        ;FAR procedure beginning
  4.     cld            ;set direction flag to forward
  5.     push    bp        ;always save frame pointer
  6.     mov    bp,sp        ;set up to get variable
  7.     mov    bx,[bp+10]    ;address of N%
  8.     mov    cx,[bx]        ;value of N% in CX
  9.     dec    cx        ;one less than N%
  10.     mov    ax,[bp+8]    ;address of pointer array P%(1)
  11.     shl    cx,1        ;multiply by 2 bytes per address
  12.     add    ax,cx        ;this is the end of pointer array
  13.     mov    [bp+10],ax    ;store end of pointers here
  14.     sub    word ptr[bp+6],3 ;start of string array table
  15.     mov    bx,[bp+8]       ;address of P%(1)
  16. s5:    xor    cx,cx        ;clear CX register
  17.     push    cx        ;save this value
  18. s3:    mov    ax,[bx]        ;value of P%(I)
  19.     mov    dx,ax        ;mulitiply X3 bytes per entry in
  20.     shl    ax,1        ;the string descriptor table
  21.     add    ax,dx        ;mult x2 and add
  22.     mov    di,[bp+6]    ;address of string table
  23.     add    di,ax        ;+ offset of table in AX
  24.     mov    cl,[di]        ;length of string
  25.     mov    si,[di+1]    ;address of A$(P(I))
  26.     mov    ax,[bx+2]    ;value of P%(I+1)
  27.     mov    dx,ax        ;again multiply x3 for
  28.     shl    ax,1        ;3 byte offset into string
  29.     add    ax,dx        ;descriptor table
  30.     push    bx        ;save the address of pointer
  31.     mov    bx,[bp+6]    ;address of string desc. table
  32.     add    bx,ax        ;+ offset of table in AX
  33.     mov    di,[bx+1]    ;address of A$(P(I+1))
  34.     cmp    cl,[bx]        ;compare lengths
  35.     jl    s1        ;put shortest length
  36.     mov    cl,[bx]        ;into CL with CH=0
  37. s1:    repz    cmpsb        ;compare strings until not equal
  38.     pop    bx        ;restore address of pointer
  39.     jle    s2        ;if not less or equal then..
  40. swap:    mov    ax,[bx]        ;swap pointer values P(I)..
  41.     mov    dx,[bx+2]    ;with P(I+1)
  42.     mov    [bx],dx
  43.     mov    [bx+2],ax
  44.     pop    ax        ;indicate that a swap took place
  45.     mov    ax,0ffffh    ;by pushing -1 onto stack
  46.     push    ax
  47. s2:    add    bx,2        ;goto the next pointer
  48.     cmp    bx,[bp+10]    ;and see if at end of array
  49.     jl    s3        ;if not go again
  50.     pop    cx        ;did a swap occur?
  51.     cmp    cx,0        ;check swap flag pushed above
  52.     jz    s4        ;if no swap we are done otherwise...
  53.     sub    word ptr [bp+10],2 ;we need to check one less string
  54.                 ;since the largest bubbled to the top
  55.     mov    bx,[bp+8]    ;retrieve end of pointer array
  56.     cmp    bx,[bp+10]    ;are we at end?
  57.     jnz    s5        ;if not do sort again
  58. s4:    pop    bp        ;restore BASIC's frame pointer
  59.     ret    6        ;return to BASIC and get rid of 6 bytes
  60.                 ;on the stack (3 arguments)
  61. sort    endp            ;close procedure
  62. cseg    ends            ;close segment
  63.     end            ;we are finished
  64.